<?php

class ChatController extends Framework_Controller_Action
{
	
	private $_chatId;
	private $_messages;
	
    public function init()
    {
    	$this->_messages = Framework_Registry::get('messages');
		$this->view->adminnum = $this->session->get('adminnum');
		if (!$this->session->get('authorize'))	{
			$this->_redirect('/admin/');
		}
    }
    
    public function indexAction()
    {
    	$this->_chatId = $this->_getParam(0,'');
    	if ($this->session->get('logged_in'.$this->_chatId)) {
			$this->_redirect('/admin/room/');
		}
		$this->view->current_page="chat";
        $this->render();
    }
	
	public function roomsAction()
	{
		$this->view->current_page="chatrooms";
		$order = $this->_getParam(0);
		$desc = $this->_getParam(1, false);
		$page = ($desc===false) ? 
					$this->_getParam(0,0)
				: $this->_getParam(2,0);
		if (!in_array ($order, array('online','room_name',
								'room_id','room_status'))) {
			$order = 'room_id';
		}
		$l = $page; 
		$p = $onpage = $this->_config->admin->on_page;
		$pages = true;
		$this->view->rooms = Room::getRooms($order,$desc,$l,$p,$pages);
		$adduri = ( isset($order) ) ? $order.'/'.$desc.'/' : '';
		$this->view->uri = $adduri;
		$this->view->pages = $pages;
		$this->view->page = $page; 
        $this->render();
	}
	
	public function deleteAction()
	{
		Room::delete(get_value($this->_getParam(0)));
		$this->_redirect('/admin/chat/rooms/');
	}
	
	/**
	 * обеспечиваем возмоность одновременного чата в нескольких окнах
	 * @param $p добавка к имени переменной сессии[optional]
	 */
	private function _login($p='')
	{
		$this->view->current_page="chat";
		if ($this->getRequest()->getMethod()=='POST') {
			if ($this->getRequest()->pass) { // Пытаемся зайти как пользователь
				$login = $this->getRequest()->getParam('login');
	            $pass = $this->getRequest()->getParam('pass');
				$user = User_Self::getCorrectUser($login, $pass);
	            if ($user instanceof User_Abstract) {
	                $this->session->set('logged_in'.$p, true);
	                $this->session->set('user_login'.$p, $login);
	                $this->session->set('user_id'.$p, $user->getUserId());
					$this->_redirect('/admin/room/');
				} else {
	                $this->view->message = $this->_messages->user->auth;       
				}
			} else {	// пытаемся зайти как гость
				$login = $this->getRequest()->getParam('login');
				 if (!($guest = User_Guest::create($login))) {
	                $this->view->message = $this->_messages->user->usedUsername;
	            } else {
	                $this->session->set('logged_in'.$p, true);
	                $this->session->set('user_login'.$p, $login);
	                $this->session->set('user_id'.$p, $guest->getUserId());
	                
	                $this->_redirect('/admin/room/');
	            }
			}
		} else {
			$this->_redirect('/admin/chat/');	
		}
	}
	
	private function _logout($p='')
	{
		$this->session->set('logged_in'.$p, false);
	}
	
	public function logoutAction()
	{
		$this->_chatId = $this->_getParam(0,'');
		$this->_logout($this->_chatId);
		$this->_redirect('/admin/room/');
	}
	
	public function loginAction()
	{
		$this->_logout($this->_chatId);
		$this->_chatId = $this->_getParam(0,'');
		$this->_login($this->_chatId);
        $this->render();
	}
	
	public function addAction()
	{
		$this->view->current_page="chatrooms";
		if ($this->getRequest()->getMethod()=='POST') {
			$name = $this->getRequest()->getParam('name');
			$status = $this->getRequest()->getParam('status');
			$status = ($status == 'on') ? 'open' : 'close';
			if (!empty($name) && !empty($status)) {
				if (Room::add($name,$status))
					$this->view->message = 'room were added';
				else
					$this->view->message = 'adding error (such room exists?)';
			}
		}
		$this->render();
	}
}
